package com.yb.web.shoper;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONArray;

import com.opensymphony.xwork2.ActionContext;
import com.yb.base.web.BaseAction;
import com.yb.bussiness.shoper.BrandService;
import com.yb.bussiness.shoper.BrandServiceImpl;
import com.yb.bussiness.shoper.PgGongLueService;
import com.yb.bussiness.shoper.PgGongLueServiceImpl;
import com.yb.domain.goods.TgBrand;
import com.yb.domain.goods.TpGongLue;
import com.yb.util.comm.RequestUtil;
import com.yb.util.framework.PageUtil;

/**
 * 拼购攻略Web
 * 
 * @author Administrator
 * 
 */
public class PgGongLueAcion extends BaseAction {
	private PgGongLueService service;
	private BrandService brandService;
	private TpGongLue tp;

	public TpGongLue getTp() {
		return tp;
	}

	public void setTp(TpGongLue tp) {
		this.tp = tp;
	}

	public PgGongLueAcion() {
		service = new PgGongLueServiceImpl();
		brandService = new BrandServiceImpl();
	}

	/**
	 * 拼购攻略保存
	 * 
	 * @return
	 */
	public String saveOrUpdate() {
		String idStr = RequestUtil.getParam(request, "tp.id", "");
		PrintWriter out = null;
		boolean isBoolean = service.addOrUpdate(tp);
		if (isBoolean)
			return "success";
		return "error";
	}

	/**
	 * 查到拼购攻略
	 */
	public String findById() {
		TpGongLue tpg = service.find();
		request.setAttribute("tp", tpg);
		return "modif";
	}

	/**
	 * 前台查询拼购攻略以及品牌
	 * 
	 * @return
	 */
	public String searchPingGouAndBrand() {
		try {
             TpGongLue tpg=	 service.find(); 
			List<TgBrand> list = brandService.findAllBrand();
			ActionContext context = ActionContext.getContext();
			context.put("tglist", list);
			request.setAttribute("tp", tpg);
		} catch (Exception e) {
			log.error("获取广告位信息error数据", e);
		}
		return "showInfo";
	}

	/**
	 * 拼购攻略左侧品牌信息列表需要加上一页及下一页
	 * 
	 * @return
	 */
	public String loadNextGroupPIC() {
		Map<String, String> parameMap = new HashMap<String, String>();
		try {
			String start = RequestUtil.getParam(request, "pageNo", "1");
			String limit = RequestUtil.getParam(request, "limit", "20");
			parameMap.put("start", start);
			parameMap.put("limit", limit);
			PageUtil pu = brandService.findAllShoperTOJulebu(parameMap);
			if (null != pu.getList() && pu.getList().size() > 0)
				response.getWriter().print(JSONArray.fromObject(pu));
			else
				response.getWriter().print("[]");
		} catch (Exception e) {
			log.error("拼购攻略左侧品牌信息列表需要加上一页及下一页", e);
			try {
				response.getWriter().print("[]");
			} catch (IOException e1) {
				e1.printStackTrace();
			}
		}
		return null;
	}

	public PgGongLueService getService() {
		return service;
	}

	public void setService(PgGongLueService service) {
		this.service = service;
	}
}
